/**
 * TableSetService.java
 *
 * Copyright (c) 2010 Prj.Manager All Rights Reserved.
 *
 * This source file may not be copied, modified or redistributed,
 * in whole or in part, in any form or for any reason, without the express
 * written consent of Prj.Manager .
 **/
package ro.prjmanager.service;


import ro.prjmanager.core.service.AbstractService;
import ro.prjmanager.core.service.Service;
import ro.prjmanager.dao.DbSchemaDao;
import ro.prjmanager.dao.ProjectDbDao;
import ro.prjmanager.dao.TableSetDao;
import ro.prjmanager.model.*;

import java.util.List;

/**
 * This class is business logic for table_set table
 * It manages all transaction maded to table_set table
 * @author Viorel Tacliciu
 * @version 2
 **/


public class TableSetService  extends AbstractService<TableSet,Integer> implements Service<TableSet,Integer> {
 
    private ProjectDbDao projectDbDao;
    private DbSchemaDao dbSchemaDao;

   

    public ProjectDbDao getProjectDbDao() {
        return projectDbDao;
    }

    public void setProjectDbDao(ProjectDbDao projectDbDao) {
        this.projectDbDao = projectDbDao;
    }

    public DbSchemaDao getDbSchemaDao() {
        return dbSchemaDao;
    }

    public void setDbSchemaDao(DbSchemaDao dbSchemaDao) {
        this.dbSchemaDao = dbSchemaDao;
    }

    public void create(TableSet user){
        String sqlDbSchema="select * from db_schema where fk_database="+((DataBases)dao.getUserSession().getDataBases()).getId()+
                " and fk_schema="+((Schema)dao.getUserSession().getSchema()).getId();
        DbSchema dbSchema=dbSchemaDao.find(sqlDbSchema).get(0);
        String sqlProjectDb="select * from project_db where fk_project="+((Projects)dao.getUserSession().getProject()).getId()+
                " and fk_db_schema="+dbSchema.getId();
        ProjectDb dbProjects=projectDbDao.find(sqlProjectDb).get(0);
        user.setFkProjectDb(dbProjects);
        dao.create(user);
    }


    public void delete(Integer id){
        TableSet delObj =new TableSet();
        delObj.setId(id);
        dao.delete(delObj);
    }

    public TableSet load(Integer id){
         return dao.find(id,false);
    }

    public List<TableSet> listPag(int startPage,int resPerPage){
          return dao.listPag(startPage, resPerPage);
    }

    public List<TableSet> listPagFilter(int startPage,int resPerPage,String filter){
          return dao.listPagFilter(startPage, resPerPage, filter);
    }

    public int no(String filter) {
        return dao.no(filter);
    }

    public void update( TableSet entity) {
        String sqlDbSchema="select * from db_schema where fk_database="+((DataBases)dao.getUserSession().getDataBases()).getId()+
                " and fk_schema="+((Schema)dao.getUserSession().getSchema()).getId();
        DbSchema dbSchema=dbSchemaDao.find(sqlDbSchema).get(0);
        String sqlProjectDb="select * from project_db where fk_project="+((Projects)dao.getUserSession().getProject()).getId()+
                " and fk_db_schema="+dbSchema.getId();
        ProjectDb dbProjects=projectDbDao.find(sqlProjectDb).get(0);
        entity.setFkProjectDb(dbProjects);
        dao.update(entity);
    }

    public List<TableSet> listPag(int startPage, int resPerPage, String orderCol, boolean asc, Object... otherItems) {
        return dao.listPag(startPage, resPerPage,orderCol,asc, otherItems);
    }

    public List<TableSet> list() {
        return dao.findAll();
    }

}
